/**
 * 所有UI的抽象
 */

const { ccclass, property } = cc._decorator;
@ccclass
export default class SdkUIBase extends cc.Component {
    private _uiData: any = null;
    private _uiName: string = "";
    @property({tooltip: "是否自动注册按钮点击事件"})
    protected autoRegisterClickEvent = true; // 是否自动注册点击事件
    
    /**子节点列表 */
    protected _nodeMaps: Map<string, cc.Node> = new Map<string, cc.Node>();
    public get nodeMaps() : Map<string, cc.Node> {
        return  this._nodeMaps;
    }
    
    public getChild(name: string) {
        return this._nodeMaps.get(name);
    }

    public setUIName(path: string) {
        this._uiName = path;
    }

    public get uiName(): string {
        return this._uiName;
    }

    public initData(param: any, uiName: string) {
        this._uiData = param;
        this._uiName = uiName;
    }

    public loadNodeMaps() {
        let self = this;
        let getChilds = function(childrens:Array<cc.Node>) {
            for (const node of childrens) {
                if(self.autoRegisterClickEvent) {
                    if (node.getComponent(cc.Button) ) {
                        let funName = node.name;
                        if (-1 != node.name.indexOf("btn_")) {
                            funName = node.name.slice(4);
                        }

                        funName += "ClickCb";

                        if (self[funName]) {
                            node.on('click', self[funName], self);
                        }
                    }

                    if (node.getComponent(cc.Slider)) {
                        let funName = node.name + "slideCb";
                        if (self[funName]) {
                            node.on("slide", self[funName], self);
                        }
                    }
                }
                self._nodeMaps.set(node.name, node);
                if (node.children.length > 0) {
                    getChilds(node.children);
                }
                
            }
        };
        let childrens = this.node.children;
        getChilds(childrens);
        // console.log("node maps ", this._nodeMaps);
    }

    protected get uiData(): any {
        return this._uiData;
    }

    // onload 之后调用
    protected show() {

    }

    // 动画结束之后回调,做界面显示操作
    public lastAnimateShow() {
    }

    protected close() {
        this.node.removeFromParent();
    }

    protected closeClickCb() {
        this.close();
    }

    protected onLoad(): void {
        this.loadNodeMaps();
        this.show();
    }

    protected onDestroy(): void {
    }

    protected onEnable(): void {}

}
